package com.woonoz.network;

import java.io.IOException;
import java.io.InputStream;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.SocketAddress;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

public class AdvancedProxySelector extends ProxySelector {

	private final PACInterpreter interpreter;
	
	public AdvancedProxySelector(final InputStream pacStream) throws InvalidPacException, IOException, PacInterpreterException {
		interpreter = new PACInterpreter(pacStream);
	}
	
	@Override
	public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
	}

	@Override
	public List<Proxy> select(final URI uri) {
		final String host = uri.getHost();
		final String url = uri.toString();
		return new ArrayList<Proxy>(interpreter.getProxyForUrl(url, host));
	}

}
